var bip39 = require('bip39')
var hdkey = require('ethereumjs-wallet/hdkey')
var util = require('ethereumjs-util')
var Tx = require('ethereumjs-tx');
var keythereum = require("keythereum");
var Web3 = require('web3');



  var web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/H2RvEhIXzZSxx73jLtcZ"))

// 合约ABI
//var abi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"burn","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_value","type":"uint256"}],"name":"burnFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"},{"name":"_extraData","type":"bytes"}],"name":"approveAndCall","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"initialSupply","type":"uint256"},{"name":"tokenName","type":"string"},{"name":"tokenSymbol","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Burn","type":"event"}];
// 合约地址
//var address = "0xf79D7D43BbAe5f64a8B2EF3bee7Cc4CfbCA5d4d7";
// 通过ABI和地址获取已部署的合约对象
//var metacoin = web3.eth.contract(abi).at(address);

//var account_one_balance = metacoin.balanceOf.call("0x7460f9d3DDB5a251c347381783740f2B06526ce3");
//console.log("account one balance: ", account_one_balance.toNumber());


//var p = {
//  "from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
//  "to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
//  "gas": "0x76c0", // 30400
//  "gasPrice": "0x9184e72a000", // 10000000000000
//  "value": "0x9184e72a", // 2441406250
//  "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
//}
//var txhash = metacoin.transfer.sendTransaction("0x7460f9d3DDB5a251c347381783740f2B06526ce3", 1000, {from:"0x3d1708d31553b1ed2e4ecd6adce0da45364f3586"});
//console.log(txhash);



//var result = web3.eth.call({
//    to: "0x5141ee4fE19c924b29d1252cD52d3FAA8BeB9Cdf", 
//    data: "0x" + "73d4a13a"
//});
//
//// Print Result
//console.log(parseInt(result, 16));


//0x3d1708d31553b1ed2e4ecd6adce0da45364f3586
var privateKey = "3b4d287748b3c38cb85b07b8850899914b8e72b56603dec93ff607f01e49b6e8"
var rawTx = {
    nonce: '0x35',
    gasPrice: '0x098bca5a00', 
    gasLimit: '0xa272',
    to: '0x5141ee4fE19c924b29d1252cD52d3FAA8BeB9Cdf', 
    value: '0x00', 
    data: '0x60fe47b1000000000000000000000000000000000000000000000000000000000000000a',
    chainId: 3
}

const tx = new Tx(rawTx)
tx.sign(Buffer.from(privateKey,'hex'))
const serializedTx = tx.serialize()

web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
    console.log(err)
    console.log(hash); //0x03df36e41621c028fbe863ec5bc12c820fe6725eb13ca7881a8f0232dc3dce30
});
